package com.project.service.impl;

import com.project.bean.BuyerCarBean;
import com.project.bean.ProductBean;
import com.project.mapper.IBuyerCarMapper;
import com.project.mapper.IProductMapper;
import com.project.service.IBuyerCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BuyerCarServiceImpl implements IBuyerCarService {
    @Autowired
    private IBuyerCarMapper buyerCarMapper;
    @Autowired
    private IProductMapper productMapper;

    @Override
    public List<BuyerCarBean> findById(Integer userId) {
        return buyerCarMapper.findByUserId(userId);
    }

    @Override
    public void add(String option, ProductBean productBean, Integer num, Integer userId, Integer money) {
        if (option.equals("add")) {
            productMapper.insert(productBean);
        }
        BuyerCarBean carBean = buyerCarMapper.findByProductId(productBean.getId(), userId);
        if (carBean == null) {
            carBean = new BuyerCarBean();
            carBean.setProductId(productBean.getId());
            carBean.setUserId(userId);
            carBean.setNum(num);
            carBean.setBuyMoney(money);

            buyerCarMapper.insert(carBean);
        } else {
            carBean.setBuyMoney(money);
            carBean.setNum(num);
            buyerCarMapper.updateById(carBean);
        }
    }

    @Override
    public void remove(Integer buyCarId) {
        buyerCarMapper.deleteById(buyCarId);
    }

    @Override
    public void updateNum(Integer buyCarId, Integer num) {
        BuyerCarBean buyerCarBean = new BuyerCarBean();
        buyerCarBean.setId(buyCarId);
        buyerCarBean.setNum(num);
        buyerCarMapper.updateById(buyerCarBean);
    }
}
